package com.band.service.impl;

import com.band.entity.*;
import com.band.mapper.EmpExprMapper;
import com.band.mapper.EmpLogMapper;
import com.band.mapper.EmpMapper;
import com.band.service.EmpService;
import com.band.utils.JwtUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpLogMapper empLogMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    /**
     * 查询员工列表
     *
     * @param param 查询参数
     * @return
     */
    @Override
    public PageBean list(EmpQueryParam param) {
        //1.设置分页参数
        PageHelper.startPage(param.getPage(), param.getPageSize());

        //2.调用mapper的列表查询方法
        List<Emp> empList = empMapper.list(param);
        Page p = (Page) empList;

        //3.封装PageBean对象并返回
        return new PageBean(p.getTotal(), p.getResult());
    }

    /**
     * 删除员工
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
        empExprMapper.delete(ids);
    }

    /**
     * 添加员工
     * @param emp
     */
    @Transactional
    @Override
    public void insert(Emp emp) {
        try {
            //补充基础属性
            emp.setPassword("123456");
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            //1.调用Mapper
            empMapper.insert(emp);
            List<EmpExpr> exprList = emp.getExprList();
            if (!CollectionUtils.isEmpty(exprList)) {
                for (EmpExpr expr : exprList) {
                    expr.setEmpId(emp.getId());
                }
                empExprMapper.insert(exprList);
            }
        } finally {
            EmpLog empLog = new EmpLog();
            empLog.setId(emp.getId());
            empLog.setOperateTime(LocalDateTime.now());
            empLog.setInfo("添加员工" + emp.getUsername());
            empLogMapper.insert(empLog);
        }

    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @Override
    public Emp select(Integer id) {
        //1.调用EmpMapper中的select方法
        Emp emp = empMapper.select(id);
        //2.调用EmpExprMapper中的select方法
        List<EmpExpr> empExpr = empExprMapper.select(id);
        emp.setExprList(empExpr);
        return emp;
    }

    /**
     * 修改员工
     * @param emp
     */
    @Transactional
    @Override
    public void update(Emp emp) {
            //1.补充基本属性
            emp.setUpdateTime(LocalDateTime.now());
            //2.调用mapper
            empMapper.update(emp);
            Integer id = emp.getId();
            empExprMapper.deleteByEmpId(id);
            List<EmpExpr> exprList = emp.getExprList();
            //3.调用EmpExprMapper中的删除方法采取先删后加原则
            if (!CollectionUtils.isEmpty(exprList)){
                for (EmpExpr expr : exprList) {
                    expr.setEmpId(id);
                }
                empExprMapper.insert(exprList);
            }

    }

    /**
     * 获取员工列表
     * @return
     */
    @Override
    public List<Emp> list() {
        return empMapper.list();
    }

    /**
     * 获取员工性别数据
     * @return
     */
    @Override
    public List<Map> empGenderData() {
        return  empMapper.empGenderData();
    }
    /**
     * 获取员工职位数据
     * @return
     */
    @Override
    public JobOption empJobData() {
        List<Map> list = empMapper.empJobData();
        List jobList = list.stream().map(item -> item.get("pow")).toList();
        List dataList = list.stream().map(item -> item.get("powCount")).toList();
        return new JobOption(jobList,dataList);
    }

    @Override
    public EmpLoginInfo login(Emp emp) {
       Emp empDB =  empMapper.login(emp);
       if (empDB != null){
           Map<String, Object> claim = new HashMap<>();
           claim.put("id", empDB.getId());
           claim.put("username", empDB.getUsername());
           String token = JwtUtils.generateJwt(claim);
           return new EmpLoginInfo(empDB.getId(), empDB.getUsername(), empDB.getName(), token);
       }

     return null;
    }


}
